package com.beadhouse.retreat.support.service;
import static com.beadhouse.common.util.AssertUtil.assertNotBlank;
import static com.beadhouse.common.util.StringUtil.blankAsNull;

import java.util.Date;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.beadhouse.common.exeption.BeadHouseException;
import com.beadhouse.entity.CustomerEntity;
import com.beadhouse.entity.RetreatEntity;
import com.beadhouse.retreat.ao.DealRetreatAo;
import com.beadhouse.retreat.ao.UpdateRetreatAo;
import com.beadhouse.retreat.dao.RetreatDao;
import com.beadhouse.retreat.dto.RetreatDto;
import com.beadhouse.retreat.service.CustomerManagementService;
import com.beadhouse.common.util.StringUtil;
import com.beadhouse.common.util.TypeTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


@Service
public class RetreatServiceImpl implements CustomerManagementService {
	@Autowired
	RetreatDao rdao;

	@Override
	public void addRetreatRecord(DealRetreatAo ao) throws Exception {
		RetreatEntity retreat=new RetreatEntity();
		assertNotBlank("创建时间必须填写",blankAsNull(ao.getCreateTime()));
		retreat.setCreateTime(TypeTime.toUtilDate(ao.getCreateTime()));
		
		assertNotBlank("退住时间必须填写",blankAsNull(ao.getRetreatTime()));
		retreat.setRetreatTime(TypeTime.toUtilDate(ao.getRetreatTime()));
		
		assertNotBlank("退住原因必须填写",blankAsNull(ao.getRetreatReason()));
		retreat.setRetreatReason(ao.getRetreatReason());
		
		assertNotBlank("退住类型必须填写",blankAsNull(ao.getRetreatType()));
		retreat.setRetreatType(ao.getRetreatType());
		
		assertNotBlank("申请人必须填写",blankAsNull(ao.getCustomerId()));
		
		
		retreat.setCustomerId(Integer.parseInt(ao.getCustomerId()));
		
		retreat.setIsDeleted(false);
		retreat.setCreateBy(1);
		retreat.setStatus("1");
		retreat.setAuditStatus("0");
		retreat.setAuditPerson("1");
		retreat.setUpdateTime(new Date());
		rdao.insert(retreat);
	}
	
	@Override
	public void deleteRetreatRecord(String RetreatId) throws Exception {
		assertNotBlank("退住id不能为空,必须填写",blankAsNull(RetreatId));
		boolean b=rdao.exists("id", RetreatId);
		if(!b) {
			throw new BeadHouseException("不存在此人的退住记录");
		}
		rdao.delete(Integer.parseInt(RetreatId));
	}

	@Override
	public void updateRetreatRecord(UpdateRetreatAo ao) throws Exception {
		RetreatEntity retreat=new RetreatEntity();

		assertNotBlank("id必须填写",blankAsNull(ao.getId()));
		retreat.setId(Integer.parseInt(ao.getId()));
		
		assertNotBlank("退住类型必须填写",blankAsNull(ao.getRetreatType()));
		retreat.setRetreatType(ao.getRetreatType());
		
		assertNotBlank("退住时间必须填写",blankAsNull(ao.getRetreatTime()));
		retreat.setRetreatTime(TypeTime.toUtilDate(ao.getRetreatTime()));
		
		assertNotBlank("更新时间必须填写",blankAsNull(ao.getUpdateTime()));
		retreat.setUpdateTime(TypeTime.toUtilDate(ao.getUpdateTime()));
		
		assertNotBlank("退住原因必须填写",blankAsNull(ao.getRetreatReason()));
		retreat.setRetreatReason(ao.getRetreatReason());
		
		if(!rdao.exists("id", retreat.getId())) {
			throw new BeadHouseException("没有查到要更新的数据");
		};
		
		rdao.update(retreat);
	}

	
	@Override
	public PageInfo<RetreatEntity> listretreatRecord(String pageNum,String key) throws Exception {
		//1验证参数
			key=StringUtil.blankAsNull(key);
			pageNum=StringUtil.blankAsNull(pageNum);
			if("null".equals(pageNum)) {
				pageNum="1";
			}
			//2执行业务逻辑
			if(key!=null) {
				key="%"+key+"%";
			}
			PageHelper.startPage(Integer.parseInt(pageNum), 3);
			List<RetreatEntity> result=rdao.selectList(key);
			PageInfo<RetreatEntity> pageInfo= new PageInfo<>(result);
			return pageInfo;
	 
	}

	
	
}
